package cmu.andersonlab.hardpropagate;

import java.io.*;

public class log {
    public long timestamp = 0;
    public long start_ms;
    private boolean use_system_time;
    private PrintWriter out;
    private File logfile;

    public static boolean exists(String name) {
        File file = new File("logs/" + name + "-log.txt");
        return file.exists();
    }

    public log (String name, boolean use_system_time) {
        File base = new File("logs/");
        // FIXME: if logs is a file then this will break
        if (!base.exists()) base.mkdir();
        logfile = new File("logs/" + name + "-log.txt");
        try {
            out = new PrintWriter(new BufferedWriter (new FileWriter(logfile, true)), true);
        } catch (IOException e) {
            System.out.println(e);
        }
        this.use_system_time = use_system_time;
        start_ms = System.currentTimeMillis();
    }

    public void close () {
        out.close();
    }

    // public void start (String... data) {
    //     out = new PrintWriter(logfile, "UTF-8");
    //     out.store ("experiment-start", data);
    // }

    public void store (String screen, String tag, String... data) {
        String acc = "";
        for (int i=0; i<data.length; i++) {
            acc += data[i];
            if (i < data.length-1) {
                acc += " ";
            }
        }
        long time = use_system_time ? System.currentTimeMillis()-start_ms:timestamp;
        out.println(String.format("(%d %s %s %s)", time, screen, tag, acc));
    }

    public static String fmtbool (boolean value) {
        return value ? "t":"nil";
    }

    public static String fmtint (int value) {
        return String.format ("%d", value);
    }

    public static String fmtstr (String value) {
        return String.format ("\"%s\"", value);
    }
}
